from SdlDotNet import *
from System.Drawing import Color, Point
from kant import KC
from page import Page

class ImagePage(Page):
    def __init__(self, title, filename, **kw):
        self.title = title
        self.filename = filename
        self.font = KC.titlefont
        Page.__init__(self)

    def init(self):
        screen = Video.Screen
        self.surf = Surface(self.filename)
        sw = screen.Width
        if self.title:
            self.title = self.font.Render(self.title, KC.titlecolour)
            if self.title.Width > sw:
                self.title.Scale(float(sw)/self.title.Width, True)

    def render(self):
        screen = Video.Screen
        sw = screen.Width
        if self.title: 
            tH = self.title.Height
            tW = self.title.Width
        else: 
            tH = tW = 0 
        screen.Blit(self.title, Point(sw/2-tW/2, 0))
        xpos = screen.Width/2 - self.surf.Width/2
        ypos = (screen.Height-tH)/2 - self.surf.Height/2
        screen.Blit(self.surf, Point(xpos, ypos))
        Page.render(self)

class TextPage(Page):
    def __init__(self, lines):
        self.lines = lines
        self.normal = []
        self.hidden = []
        self.shown = []
        self.displayed = []
        Page.__init__(self)
        self.Fonts = {'=': KC.boldfont, 
                      ':': KC.codefont, 
                      '*':KC.boldfont}
        self.Colors = {'=': KC.titlecolour, 
                       ':': KC.codecolour, 
                       '*':KC.boldcolour}
        self.dfont = KC.textfont
        self.dcolour = KC.textcolour

    def init(self):
        screen = Video.Screen
        texts = []
        for line in self.lines:
            hide = False
            font, colour = self.dfont, self.dcolour
            scale = 1
            while line and line[0] in '<>=:*-\\':
                if line[0] == '\\': 
                    break
                elif line[0] == '-': 
                    hide = True
                elif line[0] == '<': 
                    scale = scale * 0.75
                elif line[0] == '>': 
                    scale = scale * 1.33
                else:
                    font = self.Fonts.get(line[0], font)
                    colour = self.Colors.get(line[0], colour)
                line = line[1:]
            f = font.Render(line, colour)
            f.Scale(scale, True)
            texts.append((f, hide))
        # total height
        tHeight = (sum([x.Height for x,h in texts]))

        curYpos = screen.Height/2 - tHeight/2
        for t, hide in texts:
            if t is not None:
                xpos = screen.Width/2 - t.Width/2
                if hide: 
                    self.hidden.append((t, Point(xpos,curYpos)))
                else:
                    self.shown.append((t, Point(xpos,curYpos)))
            curYpos += t.Height
        self.hidden.reverse()

    def render(self):
        screen = Video.Screen
        for t,p in self.shown:
            screen.Blit(t, p)
        Page.render(self)

    def next(self):
        if self.hidden:
            h = self.hidden.pop()
            self.shown.append(h)
            self.displayed.append(h)
            return True
        else:
            return False

    def prev(self):
        if self.displayed:
            h = self.displayed.pop()
            self.shown.pop()
            self.hidden.append(h)
            return True
        else:
            return False

from codefile import CodeFilePage
from spawnpage import SpawnPage
from interp import InterpreterPage
